﻿using System.Collections.Generic;
using System.Data;
using Nvigorate.Data;

namespace Nvigorate.Test.Data.Doubles
{
    internal class TestDatabaseCommand : DatabaseCommand
    {
        #region Public Properties

        public bool UseTransactionScope { get; set; }
        public string ConnectionString { get; set; }
        public int ExecuteCalledCount { get; set; }
        public int GetTableCalledCount { get; set; }
        public int GetDataSetCalledCount { get; set; }
        public int GetScalarCalledCount { get; set; }
        public DataSet ResultSet { get; set; }

        public IDataParameterCollection Parameters
        {
            get
            {
                if (DbCommand == null) return new TestDataParameterCollection();
                return DbCommand.Parameters;
            }
        }

        private IDbCommand DbCommand
        {
            get { return _command; }
        }

        public string CommandText
        {
            get { return DbCommand == null ? null : DbCommand.CommandText; }
        }

        public string ReturnValue
        {
            get; set;
        }

        #endregion

        #region Constructors

        public TestDatabaseCommand(string command, IDatabaseProvider provider) : base(command, provider)
        {
        }

        public TestDatabaseCommand(string command, IDatabaseProvider provider,
                                   IEnumerable<IDbDataParameter> parameters) : base(command, provider, parameters)
        {
        }

        public TestDatabaseCommand(IDbCommand command, IDatabaseProvider provider) : base(command, provider){}

        #endregion

        #region Overridden Methods

        public override int Execute(IDbConnection connection, bool useTransactionScope)
        {
            ExecuteCalledCount++;
            if (connection != null)
            {
                ConnectionString = connection.ConnectionString;
            }
            UseTransactionScope = useTransactionScope;
            return 0;
        }

        public override T ExecuteCommand<T>(IDbConnection connection, bool useTransactionScope, System.Func<IDbCommand, T> commandWrapper)
        {
            return default(T);
        }

        public override DataTable GetDataTable(IDbConnection connection, string tableName)
        {
            GetTableCalledCount++;
            if (connection != null)
            {
                ConnectionString = connection.ConnectionString;
            }

            return new DataTable(tableName);
        }

        public override DataSet GetDataSet(IDbConnection connection, string datasetName, string[] tables)
        {
            GetDataSetCalledCount++;
            if (connection != null)
            {
                ConnectionString = connection.ConnectionString;
            }

            if (ResultSet != null) return ResultSet;

            var ds = new DataSet(datasetName);
            foreach (var table in tables)
            {
                ds.Tables.Add(table);
            }

            return ds;
        }

        public override object GetScalar(IDbConnection connection)
        {
            GetScalarCalledCount++;
            if (connection != null)
            {
                ConnectionString = connection.ConnectionString;
            }

            return ReturnValue;
        }
        #endregion
    }
}